<template>
  <div class="advertisingList padding_20">
    <el-select v-model="hospitalId"
               clearable
               filterable
               remote
               reserve-keyword
               placeholder="请输入关键词"
               :remote-method="remoteMethod"
               :loading="loading">
      <el-option v-for="item in options"
                 :key="item.id"
                 :label="item.name"
                 :value="item.id">
      </el-option>
    </el-select>
    <el-button @click="handleQuery"
               type="primary">查 询</el-button>
    <el-button @click="handleAdd"
               type="primary">新 增</el-button>
    <el-table stripe
              border
              :data="tableData"
              style="width: 100%"
              class="mt-20">
      <el-table-column align="center"
                       type="index"
                       min-width="20">
      </el-table-column>
      <el-table-column align="center"
                       prop="name"
                       label="广告标题"
                       min-width="100">
      </el-table-column>
      <el-table-column align="center"
                       prop="hospitalName"
                       label="医院名称"
                       min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.hospitalDto != ''">{{scope.row.hospitalDto.name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       label="广告图片"
                       width="100px"
                       heitht="50px">
        <template slot-scope="scope">
          <el-image :src="scope.row.imageUrl"
                    :preview-src-list="[scope.row.imageUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       prop="createTime"
                       label="发布时间"
                       min-width="120"
                       :formatter="formatterDate">
      </el-table-column>
      <el-table-column align="center"
                       width="180"
                       label="操作">
        <template slot-scope="scope">
          <el-button type="primary"
                     @click="handleEdit(scope.row)">编 辑
          </el-button>
          <el-button type="danger"
                     @click="handleDel(scope.row)">删 除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <page @getList="getList"
          :pageTotal="pageTotal"
          ref="page"></page>
    <el-dialog :show-close="false"
               title="新增/编辑"
               :visible.sync="dialogVisible"
               width="30%">
      <el-form :rules="rules"
               ref="form"
               :model="form"
               label-width="80px">
        <el-form-item label="医院名称"
                      prop="hospitalId">
          <el-select v-model="form.hospitalId"
                     clearable
                     filterable
                     remote
                     reserve-keyword
                     placeholder="请输入医院名称..."
                     :remote-method="remoteMethod"
                     :loading="loading">
            <el-option v-for="item in options"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="终端"
                      prop="terminal">
          <el-select v-model="form.terminal"
                     clearable
                     filterable
                     remote
                     reserve-keyword>
            <el-option v-for="item in terminalList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="广告位置"
                      prop="position">
          <el-select v-model="form.position"
                     clearable
                     filterable
                     remote
                     reserve-keyword>
            <el-option v-for="item in positionList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="Banner"
                      prop="imageUrl">
          <el-upload class="avatar-uploader"
                     :action="uploadImgUrl"
                     :show-file-list="false"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload">
            <img v-if="form.imageUrl"
                 :src="form.imageUrl"
                 class="avatar"
                 width="100px;"
                 height="50px">
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="广告链接">
          <el-input v-model="form.linkUrl"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary"
                   @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import Page from '@/base/page/page'
  import { mapGetters } from 'vuex'


  export default {
    name: "advertisingBannerList",
    data () {
      return {
        loading: false,
        tableData: [],
        dialogVisible: false,
        options: [],
        hospitalId: '',
        pageTotal: 0,
        param: {
          name: ''
        },
        terminalList: [
          {
            id: 1,
            name: '会员管家'
          },
          {
            id: 2,
            name: '机构版'
          },
          {
            id: 3,
            name: '手机个人版'
          },
          {
            id: 4,
            name: '手机医生版'
          },
          {
            id: 5,
            name: '公众号'
          },
          {
            id: 6,
            name: '小程序'
          }
        ],
        positionList: [
          {
            id: 1,
            name: '屏保'
          },
          {
            id: 2,
            name: '头部左'
          },
          {
            id: 5,
            name: '头部右'
          },
          {
            id: 3,
            name: '首页左'
          }, {
            id: 6,
            name: '首页右'
          },
          {
            id: 4,
            name: '产品学习'
          },
          {
            id: 7,
            name: '画像详情'
          },
          {
            id: 8,
            name: '药U选1'
          },
          {
            id: 9,
            name: '药U选2'
          },
          {
            id: 10,
            name: '药品列表'
          }, {
            id: 100,
            name: '个人版首页弹出广告'
          },
        ],
        form: {
          id: '',
          name: '',
          hospitalId: '',
          companyId: '',
          content: '',
          imageUrl: '',
          linkUrl: '',
          terminal: "",
          position: ""
        },
        uploadImgUrl: this.api.file.uploadImage,
        rules: {
          hospitalId: [{ required: true, message: '医院不能为空', trigger: 'blur' }],
          imageUrl: [{ required: true, message: 'Banner图片不能为空', trigger: 'blur' }],
          terminal: [{ required: true, message: '广告终端不能为空', trigger: 'blur' }],
          position: [{ required: true, message: '广告位置不能为空', trigger: 'blur' }]
        },
      }
    },
    created () {
      this.getList()
    },
    methods: {
      getList () {
        this.ajax({
          type: 'post',
          url: this.api.advertising.bannerAdvertisingList,
          data: Object.assign({}, this.page, this.param),
          success: (data) => {
            this.tableData = data.object;
            this.pageTotal = data.totalNum;
            this.$previewRefresh()
          },
          error: (data) => {
            this.$Message.error(data.message);
          }
        })
      },
      remoteMethod (query) {
        if (query !== '') {
          this.loading = true;
          this.post(this.api.searchHospital, { "pageSize": 50, "pageStart": 0, "name": query }).then((res) => {
            if (res.data.code === '200') {
              this.options = res.data.object;
            }
          });
          this.loading = false
        }
      },
      handleQuery () {
        this.page.pageIndex = 0;
        this.getList();
      },
      handleAdd () {
        this.form = {
          id: '',
          name: '',
          hospitalId: '',
          companyId: '',
          content: '',
          imageUrl: '',
          linkUrl: '',
          terminal: "",
          position: ""
        };
        this.dialogVisible = true
      },
      cancel () {
        this.dialogVisible = false
      },
      handleEdit (row) {
        this.form = row;
        this.options = [{
          id: row.hospitalId,
          name: row.hospitalDto.name,
        }];
        this.dialogVisible = true
      },
      handleSave () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.post(this.api.advertising.bannerAdvertisingEdit, this.form).then((res) => {
              if (res.data.code === '200') {
                this.$message({
                  showClose: true,
                  message: "保存成功",
                  type: 'success'
                });
                this.dialogVisible = false;
                this.getList()
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.message,
                  type: 'error'
                })
              }
            })
          }
        })
      },
      handleDel (row) {
        this.post(this.api.advertising.bannerAdvertisingDelete, row).then((res) => {
          if (res.data.code === '200') {
            this.$message({
              showClose: true,
              message: "删除成功",
              type: 'success'
            });
            this.getList();
          } else {
            this.$message({
              showClose: true,
              message: res.data.message,
              type: 'error'
            })
          }
        })
      },
      formatterDate (row) {
        return this.commonFunction.timestampFormat(new Date(row.createTime), 'yyyy-MM-dd hh:mm:ss')
      },
      handleAvatarSuccess (res) {
        this.form.imageUrl = res.url
      },
      beforeAvatarUpload (file) {
        const isLt10M = file.size / 1024 / 1024 < 20;

        /*if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }*/
        if (!isLt10M) {
          this.$message.error(' 上传图片大小不能超过2MB!');
        }
        return isLt10M;
      },
    },
    computed: {
      ...mapGetters([
        'page'
      ])
    },
    components: {
      Page,
    }
  }
</script>

<style scoped>
</style>
